<?php

namespace app\admin\model\tools;

use think\Model;


class Tools extends Model
{


    // 表名
    protected $name = 'tools';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    // 定义时间戳字段名
//    protected $createTime = false;
//    protected $updateTime = false;
//    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'status_text',
        'create_time_text',
        'update_time_text',
        'fullurl'
    ];

    public static function init()
    {
        static::afterInsert(function ($row) {
            if (!$row->weigh) {
                $row->weigh = $row->id;
                $row->save();
            }
        });
    }

    public function getSourceList()
    {
        return array_merge(['暂无'], config('site')['tools_source']);
    }

    public function getStatusList()
    {
        return ['normal' => __('Normal'), 'hidden' => __('Hidden')];
    }

    public function getZodiacList()
    {
        return array_merge(['0' => '请选择'], config('tools')['zodiac']);
    }

    public function getFlagList()
    {
        return config('site')['tools_flag'];
    }

    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getUpdateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function getFullurlAttr($value, $data)
    {
        return !empty($data['images']) ? cdnurl($data['images'], false, true) : null;
    }

    public function getFlagAttr($value)
    {
        return $value ? explode(',', $value) : null;
    }

    protected function setCreateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setUpdateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setFlagAttr($value)
    {
        return !empty($value) ? implode(',', $value) : null;
    }


}
